﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Yipusoft.SoldierArchives.BLL;

public partial class Police_Question_QuestionNew2 : System.Web.UI.Page
{
    private string ConstDepartmentID;
    protected void Page_Load(object sender, EventArgs e)
    {
        ConstDepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!IsPostBack)
        {
            if (Request["ID"] != null)
            {
                Bind();
                BindDepartmentoffer();
                bindDepartment2();
                BindDepartment();
            }

        }
    }
    private void Bind()
    {
        for (int i = 0; i < 59; i++)
        {
            if (i < 24)
            {
                dptHours.Items.Add(new ListItem(i.ToString(), i.ToString()));
                dptHours2.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            dptMine.Items.Add(new ListItem(i.ToString(), i.ToString()));
            dptMine2.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }

        QuestionDataContext db = new QuestionDataContext();
        var exam = (from e in db.Examination where e.SID == new Guid(Request["ID"]) select e).FirstOrDefault();
        var newobj = (from o in db.SecuritySubject where o.Id == new Guid(Request["ID"]) select o).Single();
        txtCaption.Text = newobj.Caption;
        txtRemark.Text = newobj.Remak;
        this.txtStartTime.Text = exam.StartTime.Value.ToString("yyyy-MM-dd");
        this.txtEndTime.Text = exam.EndTime.Value.ToString("yyyy-MM-dd");

        dptHours.SelectedValue = exam.StartTime.Value.Hour.ToString();
        dptHours2.SelectedValue = exam.EndTime.Value.Hour.ToString();
        dptMine.SelectedValue = exam.StartTime.Value.Minute.ToString();
        dptMine2.SelectedValue = exam.EndTime.Value.Minute.ToString();

        var obj = (from o in db.SecuritySubject where o.Id == new Guid(Request["ID"]) select o).FirstOrDefault();
        if (obj.CreateDepartment != ConstDepartmentID)
        {
            txtCaption.Enabled = false;
            txtEndTime.Enabled = false;
            txtRemark.Enabled = false;
            txtStartTime.Enabled = false;
            dptHours.Enabled = false;
            dptHours2.Enabled = false;
            dptMine.Enabled = false;
            dptMine2.Enabled = false;

        
        }
        
    }

    private void BindDepartmentoffer()
    {
        DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
        DepartmentList.DataBind();
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!DepartmentID.Substring(2, 3).Equals("000"))
        {
            DepartmentList.SelectedValue = DepartmentID.Substring(0, 4) + "000";
            this.DepartmentList.Enabled = false;
        }
    
        PermissionCategoryList_SelectedIndexChanged(this, new EventArgs());

    }

    private void BindDepartment()
    {
        DepartmentDataContext pt = new DepartmentDataContext();

        Soldier soldier = new Soldier();

        List<string> departmentids = soldier.GetHaveSoldierDepartmentIDs("DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' And IsDelete=0");


        var result = (from p in pt.Account_Departments where departmentids.Contains(p.DepartmentID) select p);

        this.lstDepartment.DataSource = result;
        this.lstDepartment.DataTextField = "FullName";
        this.lstDepartment.DataValueField = "DepartmentID";
        this.lstDepartment.DataBind();
    }

    void bindDepartment2()
    {
        QuestionDataContext db = new QuestionDataContext();
        DepartmentDataContext pt = new DepartmentDataContext();

        var aa = (from o in db.Examination where o.SID == new Guid(Request["ID"]) &&o.DepartmentID.Substring(0,4)==ConstDepartmentID.Substring(0,4) select o.DepartmentID).ToList();
        List<Account_Departments> dlist = (from p in pt.Account_Departments where aa.Contains(p.DepartmentID) select p).ToList();
        List<myDepartment> list = null;
        if (ViewState["Dpt"] == null)
        {
            list = new List<myDepartment>();

            foreach (Account_Departments d in dlist)
            {
                list.Add(new myDepartment() { DepartmentID = d.DepartmentID, FullName = d.FullName, Flag = "Add" });

            }

            ViewState["Dpt"] = list;

        }
        else
        {
            list = ViewState["Dpt"] as List<myDepartment>;

        }
        if (list == null)
            return;

        this.lstDepartment2.DataSource = list.Where(o=>o.Flag=="Add");
        this.lstDepartment2.DataTextField = "FullName";
        this.lstDepartment2.DataValueField = "DepartmentID";
        this.lstDepartment2.DataBind();
    }

    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        QuestionDataContext db = new QuestionDataContext();
        var exam = (from es in db.Examination where es.SID == new Guid(Request["ID"]) select es);
        DateTime St = DateTime.Parse(txtStartTime.Text);
        DateTime Et = DateTime.Parse(txtEndTime.Text);
        St = St.AddHours(double.Parse(dptHours.Text));
        St = St.AddMinutes(double.Parse(dptMine.Text));
        Et = Et.AddHours(double.Parse(dptHours.Text));
        Et = Et.AddMinutes(double.Parse(dptMine2.Text));
        foreach (Examination item in exam)
        {
            item.StartTime = St;
            item.EndTime = Et;
        }
        db.SubmitChanges();
        var newobj = (from o in db.SecuritySubject where o.Id == new Guid(Request["ID"]) select o);
        foreach (SecuritySubject sub in newobj)
        {
            sub.Caption = txtCaption.Text;
        }
        db.SubmitChanges();

        if(ViewState["Dpt"]==null)
            return;

       List<myDepartment> list = ViewState["Dpt"] as List<myDepartment>;
       string userID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID;
       foreach (myDepartment mydpt in list)
       {
           if (mydpt.Flag == "Delete")
           {
               db.SecurityResult.DeleteAllOnSubmit(db.SecurityResult.Where(o => o.SetDepartment == mydpt.DepartmentID && o.Sid == new Guid(Request["ID"])));
               db.Examination.DeleteAllOnSubmit(db.Examination.Where(o => o.DepartmentID == mydpt.DepartmentID));
               
               db.SubmitChanges();


           }
           else
           {


               Examination exam1 = new Examination();
               DateTime St1 = DateTime.Parse(txtStartTime.Text);
               DateTime Et1 = DateTime.Parse(txtEndTime.Text);
               St = St.AddHours(double.Parse(dptHours.Text));
               St = St.AddMinutes(double.Parse(dptMine.Text));
               Et = Et.AddHours(double.Parse(dptHours.Text));
               Et = Et.AddMinutes(double.Parse(dptMine2.Text));
               exam1.ID = Guid.NewGuid();
               exam1.SID = new Guid(Request["ID"]);
               exam1.DepartmentID = mydpt.DepartmentID;
               exam1.StartTime = St;
               exam1.EndTime = Et;
               exam1.SetUserID = userID;

               db.Examination.InsertOnSubmit(exam1);
               db.SubmitChanges();
           }
       }

       db.SubmitChanges();
       this.BtnAdd.Enabled = false;
    }
    protected void btnAddDepartment_Click(object sender, EventArgs e)
    {
      
        QuestionDataContext db = new QuestionDataContext();
        List<myDepartment> list = ViewState["Dpt"] as List<myDepartment>;
        if (ViewState["Dpt"] == null)
            return;

        foreach (ListItem item in lstDepartment.Items)
        {
            if (item.Selected)
            {
                if (list.Where(o => o.DepartmentID == item.Value).Count() > 0)
                {
                    myDepartment dpt = list.Where(o => o.DepartmentID == item.Value).FirstOrDefault();
                    if (dpt.Flag == "Delete")
                    {
                        dpt.Flag = "Add";
                    }

                }
                else
                {
                    list.Add(new myDepartment() { DepartmentID = item.Value, FullName = item.Text, Flag = "Add" });
                }

            }
        }
        ViewState["Dpt"] = list;
       
        BindDepartment();
        bindDepartment2();
    }

    protected void btnParent_Click(object sender, EventArgs e)
    {
        Response.Redirect("QuestionList2.aspx");
    }
    protected void PermissionCategoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDepartment();
    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        List<myDepartment> list = ViewState["Dpt"] as List<myDepartment>;
        foreach (ListItem item in lstDepartment2.Items)
        {
            if (item.Selected)
            {
                list.Where(o => o.DepartmentID == item.Value).FirstOrDefault().Flag="Delete";
               
            }
        }
        ViewState["Dpt"] = list;

        BindDepartment();
        bindDepartment2();
       
    }
}

[Serializable]
public class myDepartment
{
    public string DepartmentID
    {
        set;
        get;
    }

    public string FullName
    {
        set;
        get;

    }

    public string Flag
    {
        set;
        get;
    }

}
